package com.aube.plugin.figureex;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridLayout;

import com.aube.plugin.episode.VideoGroup;
import com.aube.plugin.episode.VideoPager;
import com.aube.plugin.model.CharacterEx;
import com.huyn.baseframework.dynamicload.DLBridge;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/9/27.
 */
public class FigureExGroupView extends GridLayout implements VideoGroup {
    private final String TAG = "Aube/FigureExGroupView";

    private Context mContext;

    private List<IPositionListener> mAnimItems = new ArrayList<>();
    private AtomicBoolean mAnimated = new AtomicBoolean(false);
    private View[] mGuideTarget;

    private VideoPager mVideoPager;
    private int mGridItemWidth, mGridItemHeight;

    public FigureExGroupView(Context context) {
        this(context, null);
    }

    public FigureExGroupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FigureExGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        initViews();
    }

    public void initViews() {
        setPadding(Utils.dip2px(mContext, 15), 0, Utils.dip2px(mContext, 15), 0);

        Display display= ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        mGridItemWidth = ( display.getWidth() - Utils.dip2px(mContext, 65) ) / 2;
        mGridItemHeight = mGridItemWidth + Utils.dip2px(mContext, 55);
        Utils.Log(TAG, "[initViews] mGridItemWidth = " + mGridItemWidth + ", mGridItemHeight = " + mGridItemHeight);
    }

    public void fillData(DLBridge bridge, List<CharacterEx> characterList) {
        if(characterList == null)
            return;

        int size = characterList.size();
        setRowCount(2);
        setColumnCount(size % 2 == 0 ? size / 2 : size / 2 + 1);
        for (int i = 0; i < size; i++) {
            FigureExGrid grid = new FigureExGrid(mContext);
            grid.initView(mGridItemWidth, mGridItemHeight);
            grid.fillData(bridge, characterList.get(i));

            int spec[] = calculateGridSpec(i);

            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
            layoutParams.topMargin = Utils.dip2px(mContext, 15);
            layoutParams.leftMargin = Utils.dip2px(mContext, 15);
            layoutParams.rowSpec = GridLayout.spec(spec[0]);
            layoutParams.columnSpec = GridLayout.spec(spec[1]);
            layoutParams.width = mGridItemWidth;
            layoutParams.height = mGridItemHeight;

            if(spec[1] == 0)
                layoutParams.leftMargin = 0;
            addView(grid, layoutParams);
        }
    }

    private int[] calculateGridSpec(int index) {
        int[] spec = new int[2];
        if(index % 4 == 1 && index > 4) {
            spec[0] = 1;
            spec[1] = index / 4 * 2;
        } else {
            spec[0] = index % 4 < 2 ? 0 : 1;
            spec[1] = index % 4 % 2 == 0 ? index / 4 * 2 :  index / 4 * 2 + 1;
        }
        return spec;
    }

    public void animInGridItems(int delay) {
        for(int i = 0; i < getChildCount(); i++) {
            FigureExGrid grid = (FigureExGrid) getChildAt(i);
            if(grid == null)
                continue;
            grid.startSlideInAnimComb(delay + i * 200);
        }
    }

    @Override
    public View[] getGuideTarget() {
        return mGuideTarget;
    }
}
